rm(list = setdiff(ls(), lsf.str()))
load("Study 1/Altered Data/Study1_Venezuela07.RData")

###Results belonging to Figure 1-------------------
#models
vote_chavez<-list()
vote_chavez[[1]]<-glm(vote_populist ~ zero1(agre) + zero1(open)+ zero1(con) +zero1(ext)+ zero1(neu)+ female+ age, data=data, family="binomial")
cl.cov_base <- cluster.vcov(vote_chavez[[1]], data$venentidad) # cluster-robust SEs for ols1
cl.robust.se.base <- sqrt(diag(cl.cov_base))


vote_chavez[[2]]<-glm(vote_populist ~ zero1(agre) + zero1(open)+ zero1(con) +zero1(ext)+ zero1(neu)+ female+ age+ income+ income_missing+ education_years+ lr_placement + lr_placement_missing , data=data, family="binomial")
cl.cov_full <- cluster.vcov(vote_chavez[[2]], data$venentidad) # cluster-robust SEs for ols1
cl.robust.se.full <- sqrt(diag(cl.cov_full))

low  <- mean(zero1(data$agre), na.rm=T)-sd(zero1(data$agre), na.rm=T)
high <- mean(zero1(data$agre), na.rm=T)+sd(zero1(data$agre), na.rm=T)

odds.difference <- (exp(vote_chavez[[2]]$coefficients)[2]*low) / (exp(vote_chavez[[2]]$coefficients)[2]*1)

labels <- c("Agreeableness", "Openness", "Conscientiousness", "Extraversion", "Neuroticism", "Female", "Age" , "Income", "Income missing (0-1)", "Education (years)", "Left-right ideology", "Ideology missing")
stargazer(vote_chavez[[1]], vote_chavez[[2]],  se=list(cl.robust.se.base, cl.robust.se.full), title="Venezuela 2006: Vote for Chavez", align=TRUE, omit.stat=c("LL","ser","f", "adj.rsq"), star.cutoffs=c(0.1, 0.05), covariate.labels=labels, dep.var.labels.include = FALSE, model.numbers= FALSE, font.size="small", column.labels = c("Base", "Figure 1"),  notes.append = FALSE, notes = "Unstandardized coefficients (logit) and clustered standard errors; *p<.1, **p<0.05", out="Tables/Venez_results.tex", no.space=TRUE, label="tab:Venezuela_results")


### Descriptive statistics----------------
desc.labels <- c("Populist vote", labels)
stargazer(vote_chavez[[2]]$model, covariate.labels=desc.labels, type = "latex", summary.stat = c("mean", "sd", "median","min",  "max"), title="Descriptive statistics LAPOP Venezuela", out="Tables/Venezuela_descrip.tex", no.space=TRUE, label="tab:Venezuela_descriptives", digits=2) 

#Alpha Agreeableness
alpha_a <- data.frame(data$a_nice_rec, data$a_kind)
psych::alpha(alpha_a)
#Alpha Neuroticism
alpha_n <- data.frame(data$n_tense, data$n_nervous_rec)
psych::alpha(alpha_n)
#Alpha Openness
alpha_o <- data.frame(data$o_thoughtful_rec, data$o_intellectual)
psych::alpha(alpha_o)
#Alpha Ext
alpha_e <- data.frame(data$e_talkative_rec, data$e_outgoing)
psych::alpha(alpha_e)
#Alpha Con
alpha_c <- data.frame(data$c_hardworker_rec, data$c_ordered)
psych::alpha(alpha_c)

### Correlation matrix-------------------------
myvars <- c("agre", "open", "con", "ext", "neu", "lr_placement")
cor_ivs <- data[myvars]

names(cor_ivs) <- c("1. Agreeableness","2. Opennesss","3. Conscientiousness",
                    "4. Extraversion", "5. Neuroticism", 
                    "6. Left-Right Ideology")
correlation.matrix <- cor(cor_ivs, use="complete.obs")
correlation.matrix <- data.frame(get_lower_tri(correlation.matrix))
correlation.matrix<-correlation.matrix[,c(1:6)]
names(correlation.matrix) <- seq(1,6,1)
correlation.matrix<-as.matrix(correlation.matrix)
stargazer(correlation.matrix, title="LAPOP Venezuela: Correlation Matrix of Independent Variables", out="Tables/Venezuela_cor.tex", no.space=TRUE, label="tab:Venezuela_cor", digits=2)